﻿using System;
using System.Collections.Generic;

namespace N2.Web
{
	using N2.Persistence;
	
	public class UriPathData: PathData
	{
		#region Properties
		
		public virtual UriTemplateMatch Match { get; protected set; }
		
		#endregion Properties

		#region Constructors
		
		internal UriPathData(
				ContentItem item,
				string templateUrl,
			UriTemplateMatch match)
			:this(item, templateUrl, DefaultAction, match)
		{
		}

		internal UriPathData(
				ContentItem item,
				string templateUrl,
				string action,
				UriTemplateMatch match)
			: base(item, templateUrl, action, string.Empty)
		{
			this.Match = match;
		}
		
		#endregion Constructors

		#region Methods

		public override PathData Detach()
		{
			PathData data = new UriPathData(
				this.CurrentItem,
				this.TemplateUrl,
				this.Action,
				this.Match);
			data.Argument = this.Argument;
			data.Path = this.Path;
			data.QueryParameters = new Dictionary<string, string>(data.QueryParameters);
			return data;
		}

		public override PathData Attach(N2.Persistence.IPersister persister)
		{
			ContentItem item = persister.Repository.Load(ID);
			PathData data = new UriPathData(item, TemplateUrl, Action, this.Match);
			data.Argument = this.Argument;
			data.QueryParameters = new Dictionary<string, string>(QueryParameters);
			return data;
		}

		#endregion Methods
	}
}
